<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>JavaScript骚操作 | 青灯有味</title>
    <meta name="generator" content="VuePress 1.5.4">
    <link rel="manifest" href="/manifest.json">
    <link rel="icon" href="/favicon.png">
    <link rel="apple-touch-icon" href="/icon_vuepress_reco.png">
    <link rel="mask-icon" href="/icon_vuepress_reco.svg" color="#42b983">
    <script language="javascript" type="text/javascript" src="https://cdn.staticfile.org/jquery/1.7.2/jquery.min.js" rel="defer"></script>
    <script language="javascript" type="text/javascript" src="/js/MouseClickEffect.js" rel="defer"></script>
    <meta name="description" content="一款简洁易懂算法博客 & 文档抠细节 & 主题">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="renderer" content="webkit">
    <meta name="force-rendering" content="webkit">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <meta name="theme-color" content="#42b983">
    <meta name="keywords" content="前端算法综合查询，办公软件，threejs3D技术开发，vue和react源码讲解，有技术的文档博客">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileColor" content="#000000">
    <meta name="baidu-site-verification" content="aO3jmjhVNd">
    <meta name="robots" content="all">
    <meta name="csdn-baidu-search" content="[object Object]">
    <link rel="preload" href="/assets/css/0.styles.1b43475e.css" as="style"><link rel="preload" href="/assets/js/app.3af45ec9.js" as="script"><link rel="preload" href="/assets/js/6.5ffa3a3c.js" as="script"><link rel="preload" href="/assets/js/1.60e80d20.js" as="script"><link rel="preload" href="/assets/js/38.27cec8e2.js" as="script"><link rel="preload" href="/assets/js/36.10d521da.js" as="script"><link rel="prefetch" href="/assets/js/10.b6c238a7.js"><link rel="prefetch" href="/assets/js/100.6fba9f18.js"><link rel="prefetch" href="/assets/js/101.79004a5d.js"><link rel="prefetch" href="/assets/js/11.a0d3dca2.js"><link rel="prefetch" href="/assets/js/12.9620ec1e.js"><link rel="prefetch" href="/assets/js/13.8a4c99c1.js"><link rel="prefetch" href="/assets/js/14.e828e497.js"><link rel="prefetch" href="/assets/js/15.85265c81.js"><link rel="prefetch" href="/assets/js/16.5b98bba6.js"><link rel="prefetch" href="/assets/js/17.5938af1b.js"><link rel="prefetch" href="/assets/js/18.24afa132.js"><link rel="prefetch" href="/assets/js/19.f978cfa2.js"><link rel="prefetch" href="/assets/js/2.3d790711.js"><link rel="prefetch" href="/assets/js/20.41e409f0.js"><link rel="prefetch" href="/assets/js/21.038c02f6.js"><link rel="prefetch" href="/assets/js/22.4595e0d7.js"><link rel="prefetch" href="/assets/js/23.6fabf12d.js"><link rel="prefetch" href="/assets/js/24.ddfa7ac5.js"><link rel="prefetch" href="/assets/js/25.fe3e21e0.js"><link rel="prefetch" href="/assets/js/26.ba28b685.js"><link rel="prefetch" href="/assets/js/27.29cb30b4.js"><link rel="prefetch" href="/assets/js/28.9dabefed.js"><link rel="prefetch" href="/assets/js/29.b110692c.js"><link rel="prefetch" href="/assets/js/3.b52271f3.js"><link rel="prefetch" href="/assets/js/30.db42d4cb.js"><link rel="prefetch" href="/assets/js/31.fe0769c9.js"><link rel="prefetch" href="/assets/js/32.fe344b91.js"><link rel="prefetch" href="/assets/js/33.09556c3f.js"><link rel="prefetch" href="/assets/js/34.0feada29.js"><link rel="prefetch" href="/assets/js/35.80a05aaf.js"><link rel="prefetch" href="/assets/js/37.bd4e8671.js"><link rel="prefetch" href="/assets/js/39.793101dd.js"><link rel="prefetch" href="/assets/js/40.0bb89ca4.js"><link rel="prefetch" href="/assets/js/41.43ba7256.js"><link rel="prefetch" href="/assets/js/42.b8b3fad3.js"><link rel="prefetch" href="/assets/js/43.b4ea7ff2.js"><link rel="prefetch" href="/assets/js/44.5d689375.js"><link rel="prefetch" href="/assets/js/45.8985fca2.js"><link rel="prefetch" href="/assets/js/46.c9058cc0.js"><link rel="prefetch" href="/assets/js/47.0e43c612.js"><link rel="prefetch" href="/assets/js/48.7fe8e1b0.js"><link rel="prefetch" href="/assets/js/49.e8a50151.js"><link rel="prefetch" href="/assets/js/50.d0966268.js"><link rel="prefetch" href="/assets/js/51.075b2486.js"><link rel="prefetch" href="/assets/js/52.104c025b.js"><link rel="prefetch" href="/assets/js/53.3e1d2136.js"><link rel="prefetch" href="/assets/js/54.75b0ddc5.js"><link rel="prefetch" href="/assets/js/55.3a80c259.js"><link rel="prefetch" href="/assets/js/56.491226d3.js"><link rel="prefetch" href="/assets/js/57.69c4f560.js"><link rel="prefetch" href="/assets/js/58.dd690d01.js"><link rel="prefetch" href="/assets/js/59.823f14a2.js"><link rel="prefetch" href="/assets/js/60.ab51afcb.js"><link rel="prefetch" href="/assets/js/61.1b6394d6.js"><link rel="prefetch" href="/assets/js/62.d0bcc3c9.js"><link rel="prefetch" href="/assets/js/63.c6f180d6.js"><link rel="prefetch" href="/assets/js/64.150fa30e.js"><link rel="prefetch" href="/assets/js/65.968b9e9f.js"><link rel="prefetch" href="/assets/js/66.1b43f384.js"><link rel="prefetch" href="/assets/js/67.fb067cd5.js"><link rel="prefetch" href="/assets/js/68.94fb1f43.js"><link rel="prefetch" href="/assets/js/69.366f59a5.js"><link rel="prefetch" href="/assets/js/7.cb2bf59a.js"><link rel="prefetch" href="/assets/js/70.9d543737.js"><link rel="prefetch" href="/assets/js/71.c2bbadfb.js"><link rel="prefetch" href="/assets/js/72.d9ad71c6.js"><link rel="prefetch" href="/assets/js/73.fdf1c18d.js"><link rel="prefetch" href="/assets/js/74.817933e2.js"><link rel="prefetch" href="/assets/js/75.1b84ada9.js"><link rel="prefetch" href="/assets/js/76.171fe943.js"><link rel="prefetch" href="/assets/js/77.02471d2d.js"><link rel="prefetch" href="/assets/js/78.9fbacaa9.js"><link rel="prefetch" href="/assets/js/79.3fe97029.js"><link rel="prefetch" href="/assets/js/8.af082f1e.js"><link rel="prefetch" href="/assets/js/80.fde761f4.js"><link rel="prefetch" href="/assets/js/81.77dc188b.js"><link rel="prefetch" href="/assets/js/82.bff1495c.js"><link rel="prefetch" href="/assets/js/83.26ffd6d9.js"><link rel="prefetch" href="/assets/js/84.b2eb10df.js"><link rel="prefetch" href="/assets/js/85.1437d92a.js"><link rel="prefetch" href="/assets/js/86.a388da50.js"><link rel="prefetch" href="/assets/js/87.07f7923d.js"><link rel="prefetch" href="/assets/js/88.c69c5036.js"><link rel="prefetch" href="/assets/js/89.7f885e62.js"><link rel="prefetch" href="/assets/js/9.d6f341ca.js"><link rel="prefetch" href="/assets/js/90.b2147a7f.js"><link rel="prefetch" href="/assets/js/91.b9378968.js"><link rel="prefetch" href="/assets/js/92.c757373d.js"><link rel="prefetch" href="/assets/js/93.05eba063.js"><link rel="prefetch" href="/assets/js/94.b1a94b72.js"><link rel="prefetch" href="/assets/js/95.a5fb7e7e.js"><link rel="prefetch" href="/assets/js/96.9b6870e4.js"><link rel="prefetch" href="/assets/js/97.353656e1.js"><link rel="prefetch" href="/assets/js/98.e3c62cd7.js"><link rel="prefetch" href="/assets/js/99.e58282f2.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.437a03e9.js">
    <link rel="stylesheet" href="/assets/css/0.styles.1b43475e.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-2d5f533b><div data-v-2d5f533b><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-2d5f533b data-v-2d5f533b><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-64685f0e data-v-2d5f533b data-v-2d5f533b><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>青灯有味</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>Mr.Zhao</span>
            
          <span data-v-64685f0e>2019 - </span>
          2023
        </a></span></div></div> <div class="hide" data-v-2d5f533b><header class="navbar" data-v-2d5f533b><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="青灯有味" class="logo"> <span class="site-name">青灯有味</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-document"></i>
      归档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/views/sofeware/" class="nav-link"><i class="iconfont undefined"></i>
  Sofeware
</a></li><li class="dropdown-item"><h4>实战项目</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/vue-manage" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  Vue项目
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="/views/demo/" class="nav-link"><i class="iconfont undefined"></i>
  Demo
</a></li><li class="dropdown-subitem"><a href="/views/project.html" class="nav-link"><i class="iconfont undefined"></i>
  我的项目
</a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/rc-bullets/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  弹幕演示
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/todolist/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  vue3-今日任务
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/react-todolist/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  React-今日任务
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/react-web/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  React响应式网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/carland/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  React carland
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/blog/" class="nav-link"><i class="iconfont undefined"></i>
  blog
</a></li><li class="dropdown-item"><!----> <a href="/categories/杂集/" class="nav-link"><i class="iconfont undefined"></i>
  杂集
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><a href="/views/theme-example.html" class="nav-link"><i class="iconfont reco-friend"></i>
  友人帐
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-other"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://www.jianshu.com/u/b7ced0328a6a" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-jianshu"></i>
  简书
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/weixin_42306796" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-csdn"></i>
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://juejin.im/user/5d39b27b51882504b7121ef5" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-juejin"></i>
  掘金
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <a href="https://github.com/zscnb/blog" target="_blank" rel="noopener noreferrer" class="repo-link"><i class="iconfont reco-github"></i>
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask" data-v-2d5f533b></div> <aside class="sidebar" data-v-2d5f533b><div class="personal-info-wrapper" data-v-ca798c94 data-v-2d5f533b><img src="/avatar.gif" alt="author-avatar" class="personal-img" data-v-ca798c94> <h3 class="name" data-v-ca798c94>
    Mr.Zhao
  </h3> <div class="num" data-v-ca798c94><div data-v-ca798c94><h3 data-v-ca798c94>23</h3> <h6 data-v-ca798c94>Article</h6></div> <div data-v-ca798c94><h3 data-v-ca798c94>33</h3> <h6 data-v-ca798c94>Tag</h6></div></div> <hr data-v-ca798c94></div> <nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-document"></i>
      归档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/views/sofeware/" class="nav-link"><i class="iconfont undefined"></i>
  Sofeware
</a></li><li class="dropdown-item"><h4>实战项目</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/vue-manage" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  Vue项目
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="/views/demo/" class="nav-link"><i class="iconfont undefined"></i>
  Demo
</a></li><li class="dropdown-subitem"><a href="/views/project.html" class="nav-link"><i class="iconfont undefined"></i>
  我的项目
</a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/rc-bullets/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  弹幕演示
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/todolist/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  vue3-今日任务
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/react-todolist/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  React-今日任务
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/react-web/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  React响应式网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="https://zscnb.gitee.io/carland/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  React carland
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/blog/" class="nav-link"><i class="iconfont undefined"></i>
  blog
</a></li><li class="dropdown-item"><!----> <a href="/categories/杂集/" class="nav-link"><i class="iconfont undefined"></i>
  杂集
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><a href="/views/theme-example.html" class="nav-link"><i class="iconfont reco-friend"></i>
  友人帐
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-other"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://www.jianshu.com/u/b7ced0328a6a" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-jianshu"></i>
  简书
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/weixin_42306796" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-csdn"></i>
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://juejin.im/user/5d39b27b51882504b7121ef5" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-juejin"></i>
  掘金
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <a href="https://github.com/zscnb/blog" target="_blank" rel="noopener noreferrer" class="repo-link"><i class="iconfont reco-github"></i>
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>久然前端算法扫盲博客</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/views/blog/" aria-current="page" class="sidebar-link">介绍</a></li><li><a href="/views/blog/algorithm.html" class="sidebar-link">算法必会篇</a></li><li><a href="/views/blog/design-modes.html" class="sidebar-link">设计模式</a></li><li><a href="/views/blog/leetcode.html" class="sidebar-link">leetcode题库</a></li><li><a href="/views/blog/Sao-operation.html" aria-current="page" class="active sidebar-link">JavaScript骚操作</a></li><li><a href="/views/blog/Source-code.html" class="sidebar-link">源码解析前端两大框架</a></li><li><a href="/views/blog/Front-end-optimization.html" class="sidebar-link">前端页面优化</a></li><li><a href="/views/blog/compoments.html" class="sidebar-link">组件库开发和发布</a></li><li><a href="/views/blog/react-base.html" class="sidebar-link">React入门到进阶</a></li><li><a href="/views/blog/react-framework.html" class="sidebar-link">React入门篇</a></li><li><a href="/views/blog/react-obtain.html" class="sidebar-link">React就业篇</a></li><li><a href="/views/blog/vue3base.html" class="sidebar-link">Vue3基本教程</a></li><li><a href="/views/blog/vue3study.html" class="sidebar-link">vue3知识梳理</a></li><li><a href="/views/blog/vuestudy.html" class="sidebar-link">学习vue常用知识点</a></li><li><a href="/views/blog/vuex.html" class="sidebar-link">Vuex</a></li><li><a href="/views/blog/Vue_performance_optimization.html" class="sidebar-link">Vue性能优化九法</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>互动3D技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Webpack</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Echarts</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-64685f0e data-v-2d5f533b><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>JavaScript骚操作</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>Mr.Zhao</span>
            
          <span data-v-64685f0e>2019 - </span>
          2023
        </a></span></div></div> <div data-v-2d5f533b><main class="page"><div class="page-title" style="display:none;"><h1 class="title">JavaScript骚操作</h1> <div data-v-3b7f5bdf><i class="iconfont reco-account" data-v-3b7f5bdf><span data-v-3b7f5bdf>Mr.Zhao</span></i> <i class="iconfont reco-date" data-v-3b7f5bdf><span data-v-3b7f5bdf>2020-03-08 16:47:56</span></i> <i class="iconfont reco-eye" data-v-3b7f5bdf><span id="/views/blog/Sao-operation.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-3b7f5bdf><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-3b7f5bdf><span class="tag-item" data-v-3b7f5bdf>重点</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><p></p><div class="table-of-contents"><ul><li><a href="#数组扁平化方法">数组扁平化方法</a><ul><li><a href="#利用函数组合">利用函数组合</a></li><li><a href="#原型链上编程">原型链上编程</a></li><li><a href="#利用数组reduce方法">利用数组reduce方法</a></li><li><a href="#利用es6方法箭头函数">利用ES6方法箭头函数</a></li></ul></li><li><a href="#模糊匹配搜索问题">模糊匹配搜索问题</a></li><li><a href="#_0-1背包问题">0-1背包问题</a><ul><li><a href="#问题描述">问题描述</a></li><li><a href="#递归">递归</a></li><li><a href="#背包问题的代码实现">背包问题的代码实现</a></li></ul></li><li><a href="#parseint函数详解">parseInt函数详解</a><ul><li><a href="#定义和用法">定义和用法</a></li><li><a href="#语法">语法</a></li><li><a href="#说明">说明</a></li><li><a href="#提示和注释">提示和注释</a></li><li><a href="#实例">实例</a></li></ul></li><li><a href="#apply-bind-call">Apply / Bind / Call</a></li><li><a href="#curry">Curry</a></li><li><a href="#pipe">Pipe</a><ul><li><a href="#代码">代码：</a></li></ul></li><li><a href="#throttle">Throttle</a><ul><li><a href="#函数节流的应用场景">函数节流的应用场景</a></li><li><a href="#实现代码">实现代码</a></li></ul></li><li><a href="#debounce">Debounce</a><ul><li><a href="#函数防抖的应用场景">函数防抖的应用场景</a></li><li><a href="#代码">代码</a></li></ul></li><li><a href="#clone">Clone</a></li><li><a href="#mixin">Mixin</a></li><li><a href="#scope">Scope</a></li></ul></div><p></p> <hr> <h2 id="数组扁平化方法"><a href="#数组扁平化方法" class="header-anchor">#</a> 数组扁平化方法</h2> <blockquote><p>数组扁平化是指将一个多维数组变为一维数组。 例如  [1, [2, 3, [4, 5]]]-- -- -- &gt; [1, 2, 3, 4, 5]</p></blockquote> <div class="language-js extra-class"><div class="highlight-lines"><br><br><br><br><br><div class="highlighted"> </div><br><br><br><br><br><br></div><pre class="language-js"><code>     <span class="token keyword">let</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'dh'</span><span class="token punctuation">,</span> <span class="token string">'fg'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">[</span>
            <span class="token punctuation">[</span>
                <span class="token punctuation">[</span>
                    <span class="token punctuation">[</span>
                        <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>
                    <span class="token punctuation">]</span>
                <span class="token punctuation">]</span>
            <span class="token punctuation">]</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>下面处理后台发来的数据， 实现数组扁平化的四种方法</strong></p> <h3 id="利用函数组合"><a href="#利用函数组合" class="header-anchor">#</a> 利用函数组合</h3> <div class="language-js extra-class"><div class="highlight-lines"><br><div class="highlighted"> </div><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div><pre class="language-js"><code>    <span class="token keyword">function</span> <span class="token function">isArray</span><span class="token punctuation">(</span><span class="token parameter">obj</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string">'[object Array]'</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">function</span> <span class="token function">flatten</span><span class="token punctuation">(</span><span class="token parameter">arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> arr <span class="token operator">=</span> arr <span class="token operator">||</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            resArr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            len <span class="token operator">=</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i <span class="token operator">+</span> <span class="token operator">+</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isArray</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                resArr <span class="token operator">=</span> resArr<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token function">flatten</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                resArr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> resArr<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token comment">// 运行结果：</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">isArray</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">[</span><span class="token string">'dg'</span><span class="token punctuation">,</span> <span class="token string">'fg'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>
</code></pre></div><h3 id="原型链上编程"><a href="#原型链上编程" class="header-anchor">#</a> 原型链上编程</h3> <blockquote><p>实现代码的复用性和高级性</p></blockquote> <div class="language-js extra-class"><div class="highlight-lines"><br><br><br><div class="highlighted"> </div><br><br><br><br><br><br><br><br></div><pre class="language-js"><code> <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">flatten</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> resArr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">ele</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string">'[object Array]'</span> <span class="token operator">?</span> resArr <span class="token operator">=</span> resArr<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>ele<span class="token punctuation">.</span><span class="token function">flatten</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">:</span> resArr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> resArr<span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>

    <span class="token comment">// 运行结果如下：</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span><span class="token function">flatten</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">[</span><span class="token string">'dg'</span><span class="token punctuation">,</span> <span class="token string">'fg'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>
</code></pre></div><h3 id="利用数组reduce方法"><a href="#利用数组reduce方法" class="header-anchor">#</a> 利用数组reduce方法</h3> <div class="language-js extra-class"><div class="highlight-lines"><br><br><br><div class="highlighted"> </div><br><br><br><br><br><br><br></div><pre class="language-js"><code>    <span class="token keyword">function</span> <span class="token function">flatten</span><span class="token punctuation">(</span><span class="token parameter">arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> arr <span class="token operator">=</span> arr <span class="token operator">||</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">prev<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>next<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string">'[object Array]'</span> <span class="token operator">?</span> prev<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token function">flatten</span><span class="token punctuation">(</span>next<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">:</span> prev<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>next<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 运行结果如下：</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">flatten</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">[</span><span class="token string">'dg'</span><span class="token punctuation">,</span> <span class="token string">'fg'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>
</code></pre></div><h3 id="利用es6方法箭头函数"><a href="#利用es6方法箭头函数" class="header-anchor">#</a> 利用ES6方法箭头函数</h3> <div class="language-js extra-class"><div class="highlight-lines"><div class="highlighted"> </div><br><br><br><br><br></div><pre class="language-js"><code>    <span class="token keyword">const</span> <span class="token function-variable function">flatten</span> <span class="token operator">=</span> <span class="token parameter">arr</span> <span class="token operator">=&gt;</span> arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">prev<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>next<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string">'[object Array]'</span> <span class="token operator">?</span> prev<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token function">flatten</span><span class="token punctuation">(</span>next<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">:</span> prev<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>next<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 运行结果如下：</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">flatten</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">[</span><span class="token string">'dg'</span><span class="token punctuation">,</span> <span class="token string">'fg'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>
</code></pre></div><hr> <h2 id="模糊匹配搜索问题"><a href="#模糊匹配搜索问题" class="header-anchor">#</a> 模糊匹配搜索问题</h2> <p><strong>代码实现</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>name<span class="token operator">:</span><span class="token string">&quot;w&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span>name<span class="token operator">:</span> <span class="token string">&quot;hggfp&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span>name<span class="token operator">:</span><span class="token string">&quot;y&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>name<span class="token operator">:</span> <span class="token string">&quot;pp&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>name<span class="token operator">:</span> <span class="token string">&quot;我pp&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>name<span class="token operator">:</span> <span class="token string">&quot;ffpp&quot;</span><span class="token punctuation">}</span><span class="token punctuation">]</span>

<span class="token keyword">let</span> dinput <span class="token operator">=</span> <span class="token punctuation">{</span>name<span class="token operator">:</span> <span class="token string">&quot;p&quot;</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">search</span><span class="token punctuation">(</span><span class="token parameter">arr<span class="token punctuation">,</span> value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	
	<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token operator">||</span> arr<span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">return</span> arr
	
	<span class="token keyword">let</span> index <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
	<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		
		<span class="token comment">//如果不存在的话，直接跳过这个i，进行下一i</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>name<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>dinput<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
		
		<span class="token comment">//获得用户输入的值在目前的值索引，然后排序，然后输出新排序的数组</span>
		index<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
			id<span class="token operator">:</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>name<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>value<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">,</span>
			v<span class="token operator">:</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
		<span class="token punctuation">}</span><span class="token punctuation">)</span>
		
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token function">myarr</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">myarr</span><span class="token punctuation">(</span><span class="token parameter">index</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">let</span> newArr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">let</span> arr1 <span class="token operator">=</span> index<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token function">mysort</span><span class="token punctuation">(</span><span class="token string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr1<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
		newArr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>arr1<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>v<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> newArr
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">mysort</span><span class="token punctuation">(</span><span class="token parameter">prop</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> a<span class="token punctuation">[</span>prop<span class="token punctuation">]</span> <span class="token operator">-</span> b<span class="token punctuation">[</span>prop<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">search</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> dinput<span class="token punctuation">)</span><span class="token punctuation">)</span>

输出：

<span class="token punctuation">[</span> <span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token string">'pp'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token string">'我pp'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token string">'ffpp'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token string">'hggfp'</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span>
</code></pre></div><hr> <h2 id="_0-1背包问题"><a href="#_0-1背包问题" class="header-anchor">#</a> 0-1背包问题</h2> <h3 id="问题描述"><a href="#问题描述" class="header-anchor">#</a> 问题描述</h3> <p>有n个物品，它们有各自的体积和价值，现有给定容量的背包，如何让背包里装入的物品具有最大的价值总和？</p> <p><img src="/assets/img/knapsacks.22c4f19e.png" alt=""></p> <p>对于每一件物品，可以有不拿(0)和拿(1)两种选择，因此该问题通常称之为“0-1背包问题”</p> <p>“0-1背包问题”需要使用<strong>递归</strong>求解</p> <h3 id="递归"><a href="#递归" class="header-anchor">#</a> 递归</h3> <p>递归（recursive）是指某个函数在执行过程中直接或间接的调用自身</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token constant">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//...</span>
    <span class="token constant">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//产生递归</span>
<span class="token punctuation">}</span>
</code></pre></div><p>如何理解递归的函数调用呢？</p> <p>每一次函数调用，JS会创建一个执行上下文，并放置到执行栈中执行。</p> <ul><li><strong>执行上下文</strong>：每当<strong>调用</strong>一个函数，会创建一个该函数的执行上下文，上下文中包含该函数的相关参数、变量</li> <li><strong>执行栈</strong>：执行上下文会放置到执行栈中[^1]，每当执行上下文创建时，会进入执行栈；每当函数调用完成后，会从执行栈中移除。在刚开始执行JS时，会创建一个全局上下文。</li></ul> <p>例如下面的代码：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>n <span class="token operator">&lt;=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> n <span class="token operator">*</span> <span class="token function">test</span><span class="token punctuation">(</span>n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">test</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>整个的执行过程见ppt</p> <p>理解执行栈，最重要的是理解<strong>每次函数的执行是独立的、互不干扰的</strong>，哪怕调用的是同一个函数。</p> <p>有些时候，使用递归可以让我们用极其简洁的代码来表达程序逻辑，例如上面的代码，就是求阶乘的函数。</p> <p>使用递归时，要注意函数的终止，否则将导致无限递归，从而使执行栈溢出。</p> <h3 id="背包问题的代码实现"><a href="#背包问题的代码实现" class="header-anchor">#</a> 背包问题的代码实现</h3> <p><img src="/assets/img/knapsacks.22c4f19e.png" alt=""></p> <p><strong>物品如何表示？</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> objects <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span> volume<span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">//物品对象，value表示价值，volume表示体积</span>
    <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span> volume<span class="token operator">:</span> <span class="token number">3</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token number">7</span><span class="token punctuation">,</span> volume<span class="token operator">:</span> <span class="token number">4</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token number">8</span><span class="token punctuation">,</span> volume<span class="token operator">:</span> <span class="token number">5</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> value<span class="token operator">:</span> <span class="token number">9</span><span class="token punctuation">,</span> volume<span class="token operator">:</span> <span class="token number">6</span> <span class="token punctuation">}</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>实现函数如何表示？</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * 求解背包问题
 * @param capacity 背包容量，例：12
 * @param objects 可选物品
 * @return 求解结果，一个对象，包含3个信息：1. 选择的物品数组   2. 总价值  3. 总占用空间
 * 例如： {
 *      objects: [{ value: 4, volume: 2 }, { value: 5, volume: 3 }],
 *      value: 9,
 *      volume: 5
 * }
 */</span>
<span class="token keyword">function</span> <span class="token function">getMaxValue</span><span class="token punctuation">(</span><span class="token parameter">capacity<span class="token punctuation">,</span> objects</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

<span class="token punctuation">}</span>
</code></pre></div><p><strong>如何实现？</strong></p> <ol><li>特殊情况： 没有可选物品</li> <li>普通情况： 要将问题转换为 是否要选择第一件物品 的问题</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">if</span><span class="token punctuation">(</span>第一件物品装不下<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">return</span> 同样的空间装剩余物品<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> result1 <span class="token operator">=</span> 装第一件物品的结果<span class="token punctuation">;</span>
<span class="token keyword">var</span> result2 <span class="token operator">=</span> 不装第一件物品的结果<span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>result1<span class="token punctuation">.</span>value <span class="token operator">&gt;</span> result2<span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//比较总价值</span>
    <span class="token keyword">return</span> result1<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span><span class="token punctuation">{</span>
    <span class="token keyword">return</span> result2<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p><strong>代码实现</strong></p> <div class="language-js extra-class"><pre class="language-js"><code>        <span class="token keyword">var</span> objects <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
                value<span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
                volume<span class="token operator">:</span> <span class="token number">2</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">//物品对象，value表示价值，volume表示体积</span>
            <span class="token punctuation">{</span>
                value<span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
                volume<span class="token operator">:</span> <span class="token number">3</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                value<span class="token operator">:</span> <span class="token number">20</span><span class="token punctuation">,</span>
                volume<span class="token operator">:</span> <span class="token number">10</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                value<span class="token operator">:</span> <span class="token number">7</span><span class="token punctuation">,</span>
                volume<span class="token operator">:</span> <span class="token number">4</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                value<span class="token operator">:</span> <span class="token number">8</span><span class="token punctuation">,</span>
                volume<span class="token operator">:</span> <span class="token number">5</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                value<span class="token operator">:</span> <span class="token number">9</span><span class="token punctuation">,</span>
                volume<span class="token operator">:</span> <span class="token number">6</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">]</span><span class="token punctuation">;</span>

        <span class="token comment">/**
         * 求解背包问题
         * @param capacity 背包容量，例：12
         * @param objects 可选物品
         * @return 求解结果，一个对象，包含3个信息：1. 选择的物品数组   2. 总价值  3. 总占用空间
         * 例如： {
         *      objects: [{ value: 4, volume: 2 }, { value: 5, volume: 3 }], //选择的物品
         *      value: 9, //选择的物品总价值是多少
         *      volume: 5 //选择的物品占用的总空间
         * }
         */</span>
        <span class="token keyword">function</span> <span class="token function">getMaxValue</span><span class="token punctuation">(</span><span class="token parameter">capacity<span class="token punctuation">,</span> objects</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>objects<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">//没有可选物品</span>
                <span class="token keyword">return</span> <span class="token punctuation">{</span>
                    objects<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                    value<span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
                    volume<span class="token operator">:</span> <span class="token number">0</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
            <span class="token comment">//有可选的物品</span>
            <span class="token comment">//就看第一件物品能不能选</span>
            <span class="token keyword">var</span> obj <span class="token operator">=</span> objects<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//第1件物品</span>
            <span class="token keyword">var</span> left <span class="token operator">=</span> objects<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//除去第一件物品剩余的物品</span>
            
            <span class="token comment">//装不下第一件物品，不装</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>obj<span class="token punctuation">.</span>volume <span class="token operator">&gt;</span> capacity<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token function">getMaxValue</span><span class="token punctuation">(</span>capacity<span class="token punctuation">,</span> left<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//空间不变，第一件物品去掉</span>
            <span class="token punctuation">}</span>

            <span class="token comment">// 装第一件物品的背包结果</span>
            <span class="token keyword">var</span> result1 <span class="token operator">=</span> <span class="token function">getMaxValue</span><span class="token punctuation">(</span>capacity <span class="token operator">-</span> obj<span class="token punctuation">.</span>volume<span class="token punctuation">,</span> left<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//得到剩余空间、剩余物品的最优解</span>
            result1<span class="token punctuation">.</span>objects<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//选择的物品数组中加入第一件物品</span>
            result1<span class="token punctuation">.</span>value <span class="token operator">+=</span> obj<span class="token punctuation">.</span>value<span class="token punctuation">;</span> <span class="token comment">//总价值加上第一件物品</span>
            result1<span class="token punctuation">.</span>volume <span class="token operator">+=</span> obj<span class="token punctuation">.</span>volume<span class="token punctuation">;</span> <span class="token comment">//总空间加上第一件物品</span>
            
            <span class="token comment">//不装第一件物品的背包结果</span>
            <span class="token keyword">var</span> result2 <span class="token operator">=</span> <span class="token function">getMaxValue</span><span class="token punctuation">(</span>capacity<span class="token punctuation">,</span> left<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//得到空间不变、剩余物品的最优解</span>
            
            <span class="token comment">//比较总价值</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>result1<span class="token punctuation">.</span>value <span class="token operator">&gt;</span> result2<span class="token punctuation">.</span>value<span class="token punctuation">)</span> <span class="token punctuation">{</span> 
                <span class="token keyword">return</span> result1<span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> result2<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">var</span> result <span class="token operator">=</span> <span class="token function">getMaxValue</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">,</span> objects<span class="token punctuation">)</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//{objects: Array(2), value: 24, volume: 12}</span>
        <span class="token comment">//objects: Array(2)</span>
        <span class="token comment">//0: {value: 4, volume: 2}</span>
        <span class="token comment">//1: {value: 20, volume: 10}</span>
        <span class="token comment">//length: 2</span>
        <span class="token comment">//__proto__: Array(0)</span>
        <span class="token comment">//value: 24</span>
        <span class="token comment">//volume: 12</span>
        <span class="token comment">//__proto__: Object</span>
</code></pre></div><hr> <h2 id="parseint函数详解"><a href="#parseint函数详解" class="header-anchor">#</a> parseInt函数详解</h2> <h3 id="定义和用法"><a href="#定义和用法" class="header-anchor">#</a> 定义和用法</h3> <p><code>parseInt() 函数可解析一个字符串，并返回一个十进制整数。如果解析一个整数的话，先转化为字符串toString，再解析</code></p> <h3 id="语法"><a href="#语法" class="header-anchor">#</a> 语法</h3> <p><code>parseInt(string, radix)</code></p> <p>参数	描述</p> <p>string	必需。要被解析的字符串。</p> <p>radix	
可选。表示要解析的数字的基数。<code>该值介于 2 ~ 36 之间。</code></p> <p>如果省略该参数或其值为 0，则数字将以 10 为基础来解析。如果它以 “0x” 或 “0X” 开头，将以 16 为基数。</p> <p>如果该参数小于 2 或者大于 36，则 parseInt() 将返回 NaN。</p> <p><code>返回值：必须是十进制的整数，否则返回NaN</code></p> <p><strong>返回解析后的数字</strong></p> <h3 id="说明"><a href="#说明" class="header-anchor">#</a> <strong>说明</strong></h3> <p>当参数 radix 的值为 0，或没有设置该参数时，parseInt() 会根据 string 来判断数字的基数。</p> <p><strong>举例</strong></p> <p>如果 string<code>以 &quot;0x&quot; 开头</code>，parseInt() 会把 string 的其余部分解析为十六进制的整数。</p> <p>如果 string 以 0 开头，那么 ECMAScript v3 允许 parseInt() 的一个实现把其后的字符解析为八进制或十六进制的数字。</p> <p>如果 string 以 1 ~ 9 的数字开头，parseInt() 将把它解析为十进制的整数。</p> <h3 id="提示和注释"><a href="#提示和注释" class="header-anchor">#</a> 提示和注释</h3> <p>注释：只有字符串中的第一个数字会被返回。</p> <p>注释：开头和结尾的空格是允许的。</p> <p>提示：如果字符串的第一个字符不能被转换为数字，那么 parseFloat() 会返回 NaN。</p> <h3 id="实例"><a href="#实例" class="header-anchor">#</a> 实例</h3> <p>在本例中，我们将使用 parseInt() 来解析不同的字符串：</p> <p><strong>例1</strong>:</p> <div class="language- extra-class"><pre class="language-text"><code>parseInt('123px') =&gt; 返回123
parseInt('345.34bv') =&gt; 返回345
parseInt('abc123') =&gt; 返回NaN
</code></pre></div><p><strong>例2</strong>：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">&quot;10&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token comment">//返回10</span>
<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>十进制范围<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token comment">//parseInt(&quot;10&quot;) = parseInt(&quot;10&quot;,10),因为1和0都在十进制范围内，所以以‘10’为十进制数返回十进制数1*10^1 + 0 * 10^0 = 10 + 0 = 10</span>

<span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">&quot;19&quot;</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//返回 19 (10+9)</span>
<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>十进制范围<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token comment">//parseInt(&quot;19&quot;，10),因为1和9都在十进制范围内，所以以‘19’为十进制数返回十进制数1*10^1 + 9 * 10^0 = 10 + 9 = 19</span>

<span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">&quot;11&quot;</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//返回 3 (2+1)</span>
<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>二进制范围<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token comment">//parseInt(&quot;11&quot;，2),因为1和1都在二进制范围内，所以以‘11’为二进制数返回十进制数1 * 2^1 + 1 * 2^0 = 2 + 1 = 3</span>

<span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">&quot;17&quot;</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//返回 15 (8+7)</span>
<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>八进制范围<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token comment">//parseInt(&quot;17&quot;，8),因为1和7都在八进制范围内，所以以‘17’为八进制数返回十进制数1 * 8^1 + 7 * 8^0 =  8 + 7 = 15</span>

<span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">&quot;1f&quot;</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//返回 31 (16+15)</span>
<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>十六进制范围<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">15</span><span class="token punctuation">]</span><span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token comment">//parseInt(&quot;1f&quot;，16),因为1和 f= 15都在16进制范围内，所以以‘1f’为16进制数返回十进制数1 * 16^1 + 15 * 16^0 = 16 + 15 = 31</span>

<span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">&quot;010&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//未定：返回 10 或 8</span>
ie8返回<span class="token number">8</span>，其他浏览器返回<span class="token number">10</span>

</code></pre></div><p><strong>例3</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> result <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'10'</span><span class="token punctuation">,</span> <span class="token string">'11'</span><span class="token punctuation">,</span> <span class="token string">' 1152'</span><span class="token punctuation">,</span> <span class="token string">'13'</span><span class="token punctuation">,</span> <span class="token string">'123'</span><span class="token punctuation">,</span> <span class="token string">'147'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>parseInt<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
<span class="token comment">//[ 10, NaN, 3, 1, 27, 9 ]</span>

详解：

 <span class="token punctuation">[</span><span class="token string">'10'</span><span class="token punctuation">,</span> <span class="token string">'11'</span><span class="token punctuation">,</span> <span class="token string">'1152'</span><span class="token punctuation">,</span> <span class="token string">'13'</span><span class="token punctuation">,</span><span class="token string">'123'</span><span class="token punctuation">,</span><span class="token string">'147'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">ele<span class="token punctuation">,</span> i</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
     <span class="token keyword">return</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>ele<span class="token punctuation">,</span> i<span class="token punctuation">)</span>
 <span class="token punctuation">}</span>
 
 <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>十进制范围<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">--</span><span class="token operator">&gt;</span>
 <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'10'</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'10'</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>因为<span class="token number">1</span>和<span class="token number">0</span>都在十进制范围内，所以以‘<span class="token number">10</span>’为十进制数返回十进制数<span class="token number">1</span><span class="token operator">*</span><span class="token number">10</span><span class="token operator">^</span><span class="token number">1</span> <span class="token operator">+</span> <span class="token number">0</span> <span class="token operator">*</span> <span class="token number">10</span><span class="token operator">^</span><span class="token number">0</span> <span class="token operator">=</span> <span class="token number">10</span> <span class="token operator">+</span> <span class="token number">0</span> <span class="token operator">=</span> <span class="token number">10</span>
 
 <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'11'</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> radix介于 <span class="token number">2</span> <span class="token operator">~</span> <span class="token number">36</span> 之间<span class="token punctuation">,</span>否则返回<span class="token number">NaN</span>，<span class="token number">1</span>不在<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">36</span><span class="token punctuation">]</span><span class="token punctuation">,</span>故返回<span class="token number">NaN</span>
 
 <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>识别字符的顺序从右往左的<span class="token operator">--</span><span class="token operator">&gt;</span>
 <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>二进制范围<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">--</span><span class="token operator">&gt;</span>
 <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'1152'</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> 因为<span class="token number">1</span>和<span class="token number">1</span>都在二进制范围内，<span class="token number">5</span>和<span class="token number">2</span>不在二进制范围之外故舍去，所以以‘<span class="token number">11</span>’为二进制数返回十进制数<span class="token number">1</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token operator">^</span><span class="token number">1</span> <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token operator">^</span><span class="token number">0</span> <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">=</span> <span class="token number">3</span>
 
  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>识别字符的顺序从右往左的<span class="token operator">--</span><span class="token operator">&gt;</span>
 <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>三进制范围<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token operator">--</span><span class="token operator">&gt;</span>
 <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'13'</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>因为<span class="token number">3</span>不在三进制范围内故舍去，所以以‘<span class="token number">1</span>’为三进制数返回十进制数<span class="token number">1</span><span class="token operator">*</span> <span class="token number">3</span><span class="token operator">^</span><span class="token number">0</span> <span class="token operator">=</span> <span class="token number">1</span>
   
<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>四进制范围<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token parameter"><span class="token number">123</span><span class="token punctuation">,</span> <span class="token number">4</span></span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token function">首先把toString</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span>转为字符串，因为<span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span>都在四进制范围之内，所以以‘<span class="token number">123</span>’为四进制数返回十进制<span class="token number">1</span><span class="token operator">*</span> <span class="token number">4</span><span class="token operator">^</span><span class="token number">2</span> <span class="token operator">+</span> <span class="token number">2</span> <span class="token operator">*</span> <span class="token number">4</span><span class="token operator">^</span><span class="token number">1</span> <span class="token operator">+</span> <span class="token number">3</span> <span class="token operator">*</span><span class="token number">4</span><span class="token operator">^</span><span class="token number">0</span> <span class="token operator">=</span> <span class="token number">16</span> <span class="token operator">+</span> <span class="token number">8</span> <span class="token operator">+</span> <span class="token number">3</span> <span class="token operator">=</span> <span class="token number">27</span>
    
 <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>识别字符的顺序从右往左的<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>五进制范围<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token operator">--</span><span class="token operator">&gt;</span>    
<span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'147'</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> 因为<span class="token number">1</span> 和<span class="token number">4</span> 在五进制范围之内，<span class="token number">7</span>不在范围之内故舍去，所以以‘<span class="token number">14</span>’为五进制数返回十进制<span class="token number">1</span><span class="token operator">*</span> <span class="token number">5</span><span class="token operator">^</span><span class="token number">1</span> <span class="token operator">+</span> <span class="token number">4</span> <span class="token operator">*</span> <span class="token number">5</span><span class="token operator">^</span><span class="token number">0</span> <span class="token operator">=</span> <span class="token number">5</span> <span class="token operator">+</span> <span class="token number">4</span> <span class="token operator">=</span> <span class="token number">9</span>   
 
</code></pre></div><h2 id="apply-bind-call"><a href="#apply-bind-call" class="header-anchor">#</a> Apply / Bind / Call</h2> <div class="language-js extra-class"><pre class="language-js"><code>
<span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">Apply</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span>arr</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    ctx<span class="token punctuation">.</span>func <span class="token operator">=</span> <span class="token keyword">this</span>
    <span class="token keyword">let</span> res <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">func</span><span class="token punctuation">(</span><span class="token operator">...</span>arr<span class="token punctuation">)</span>
    <span class="token keyword">delete</span> ctx<span class="token punctuation">.</span>func
    <span class="token keyword">return</span> res
<span class="token punctuation">}</span>

<span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">Call</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span><span class="token operator">...</span>args</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    ctx<span class="token punctuation">.</span>func <span class="token operator">=</span> <span class="token keyword">this</span>
    <span class="token keyword">let</span> res <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">func</span><span class="token punctuation">(</span><span class="token operator">...</span>args<span class="token punctuation">)</span>
    <span class="token keyword">delete</span> ctx<span class="token punctuation">.</span>func
    <span class="token keyword">return</span> res
<span class="token punctuation">}</span>

<span class="token comment">//bind ES6版</span>
<span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">Bind</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span><span class="token operator">...</span>args1</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">let</span> self <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
    <span class="token keyword">function</span> <span class="token function">func</span><span class="token punctuation">(</span><span class="token parameter"><span class="token operator">...</span>args2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token function">self</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">this</span> <span class="token keyword">instanceof</span> <span class="token class-name">func</span> <span class="token operator">?</span> <span class="token keyword">this</span> <span class="token operator">:</span> ctx<span class="token punctuation">,</span><span class="token operator">...</span>args1<span class="token punctuation">,</span><span class="token operator">...</span>args2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    func<span class="token punctuation">.</span>prototype <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>prototype<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> func<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">//bind ES5版</span>
<span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">Bind</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">var</span> self <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> args <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>arguments<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">var</span> <span class="token function-variable function">temp</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

    <span class="token keyword">function</span> <span class="token function">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> bindArgs <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token function">self</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">this</span> <span class="token keyword">instanceof</span> <span class="token class-name">temp</span> <span class="token operator">?</span> <span class="token keyword">this</span> <span class="token operator">:</span> context<span class="token punctuation">,</span> args<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>bindArgs<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    temp<span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>prototype<span class="token punctuation">;</span>
    func<span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">temp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> func<span class="token punctuation">;</span>
<span class="token punctuation">}</span>


</code></pre></div><h2 id="curry"><a href="#curry" class="header-anchor">#</a> Curry</h2> <p><strong><code>柯里化</code> ，英语：Currying(果然是满满的英译中的既视感)，是把接受多个参数的函数变换成接受一个单一参数（最初函数的第一个参数）的函数，并且返回接受余下的参数而且返回结果的新函数的技术。</strong></p> <p><code>与bind实现差不多</code></p> <h4 id="柯里化函数优点"><a href="#柯里化函数优点" class="header-anchor">#</a> 柯里化函数优点</h4> <ol><li><b style="color:#00b77f;">参数复用</b></li> <li><b style="color:#00b77f;">提前确认参数</b></li> <li><b style="color:#00b77f;">延迟执行，返回新函数</b></li></ol> <h4 id="代码"><a href="#代码" class="header-anchor">#</a> 代码</h4> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/** 
 *  柯里化函数：固定某个函数的一些参数，得到该函数剩余参数的一个新函数，如果没有剩余参数，则直接调用
 * 在函数式编程中，科里化最重要的作用是把多参函数变为单参函数
 */</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin<span class="token punctuation">.</span><span class="token function-variable function">curry</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">func</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 首先调用函数参数</span>
        <span class="token comment">//得到从下标1开始的参数</span>
        <span class="token keyword">var</span> args <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>arguments<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> that <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// 再次调用函数参数</span>
            <span class="token keyword">var</span> curArgs <span class="token operator">=</span> Array<span class="token punctuation">.</span><span class="token function">from</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//当前调用的参数</span>
            <span class="token keyword">var</span> totalArgs <span class="token operator">=</span> args<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>curArgs<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>totalArgs<span class="token punctuation">.</span>length <span class="token operator">&gt;=</span> func<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">//参数数量够了</span>
                <span class="token keyword">return</span> <span class="token function">func</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> totalArgs<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                <span class="token comment">//参数数量仍然不够</span>
                totalArgs<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span>func<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> that<span class="token punctuation">.</span><span class="token function">curry</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span>that<span class="token punctuation">,</span> totalArgs<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code> <span class="token comment">// curry：  Haskell curry</span>
        <span class="token comment">// 科里化函数：固定某个函数的一些参数，得到该函数剩余参数的一个新函数，如果没有剩余参数，则调用</span>

        <span class="token keyword">function</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> z</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token punctuation">(</span>x <span class="token operator">+</span> y<span class="token punctuation">)</span> <span class="token operator">*</span> z<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// 求：(2+3)*5  (2+5)*6  (2+4)*7  (2+4)*16</span>
        <span class="token keyword">var</span> g <span class="token operator">=</span> myPlugin<span class="token punctuation">.</span><span class="token function">curry</span><span class="token punctuation">(</span>f<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">g</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">g</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">var</span> y <span class="token operator">=</span> <span class="token function">g</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// var y = myPlugin.curry(f, 2, 4)</span>

        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">y</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">y</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="pipe"><a href="#pipe" class="header-anchor">#</a> Pipe</h2> <div class="custom-block tip"><p>函数管道：将多个单参函数组合起来，形成一个新的函数，这些函数中，前一个函数的输出，是后一个函数的输入</p></div> <h3 id="代码-2"><a href="#代码-2" class="header-anchor">#</a> 代码：</h3> <div class="language- extra-class"><pre class="language-text"><code>if (!this.myPlugin) {
    this.myPlugin = {};
}

this.myPlugin.pipe = function() {
    var args = Array.from(arguments);
    return function(val) {
        
        //简化写法
        return args.reduce(function(result, func) {
            return func(result);
        }, val);

        &lt;!-- 普通写法 --&gt;
        // for (var i = 0; i &lt; args.length; i++) {
        //     var func = args[i];
        //     val = func(val);
        // }
        // return val;
    }
}
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code>
        <span class="token comment">//将字符串中每一个单词首字母大写</span>
        <span class="token keyword">function</span> <span class="token function">everyFirstLetterUp</span><span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/\b(\w)(\w*)\b/g</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">$<span class="token punctuation">,</span> $<span class="token number">1</span><span class="token punctuation">,</span> $<span class="token number">2</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> $<span class="token number">1.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> $<span class="token number">2</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">//将字符串中除首字母外的其他字母小写</span>
        <span class="token keyword">function</span> <span class="token function">otherLetterLower</span><span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/\b(\w)(\w*)\b/g</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">$<span class="token punctuation">,</span> $<span class="token number">1</span><span class="token punctuation">,</span> $<span class="token number">2</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> $<span class="token number">1</span> <span class="token operator">+</span> $<span class="token number">2.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">//去掉字符串中所有的空白字符</span>
        <span class="token keyword">function</span> <span class="token function">removeEmpty</span><span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/\s+/g</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">//将字符串中第一个单词字符小写</span>
        <span class="token keyword">function</span> <span class="token function">firstLetterLower</span><span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/\w/</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">$</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> $<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">//截取字符串</span>
        <span class="token keyword">function</span> <span class="token function">curString</span><span class="token punctuation">(</span><span class="token parameter">number<span class="token punctuation">,</span> str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> number<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">//将一个字符串变为小驼峰命名法</span>

        <span class="token keyword">var</span> smallCamel <span class="token operator">=</span> myPlugin<span class="token punctuation">.</span><span class="token function">pipe</span><span class="token punctuation">(</span>everyFirstLetterUp<span class="token punctuation">,</span> firstLetterLower<span class="token punctuation">,</span>
            otherLetterLower<span class="token punctuation">,</span> removeEmpty<span class="token punctuation">,</span> myPlugin<span class="token punctuation">.</span><span class="token function">curry</span><span class="token punctuation">(</span>curString<span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">smallCamel</span><span class="token punctuation">(</span><span class="token string">&quot;    MY     fIrST    naME&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//myFirstNam</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">smallCamel</span><span class="token punctuation">(</span><span class="token string">&quot;  asdfasf  sdfasf   ggggggg  hGDGF&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//asdfasfSdf</span>
</code></pre></div><h2 id="throttle"><a href="#throttle" class="header-anchor">#</a> Throttle</h2> <p><strong>限制一个函数在一定时间内只能执行一次。</strong></p> <p>举个例子，乘坐地铁，过闸机时，每个人进入后3秒后门关闭，等待下一个人进入。</p> <h3 id="函数节流的应用场景"><a href="#函数节流的应用场景" class="header-anchor">#</a> 函数节流的应用场景</h3> <p>间隔一段时间执行一次回调的场景有：</p> <ul><li>滚动加载，加载更多或滚到底部监听</li> <li>谷歌搜索框，搜索联想功能</li> <li>高频点击提交，表单重复提交</li></ul> <h3 id="实现代码"><a href="#实现代码" class="header-anchor">#</a> 实现代码</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * 函数节流 保证一段时间内只执行一次
 */</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin<span class="token punctuation">.</span><span class="token function-variable function">throttle</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">callback<span class="token punctuation">,</span> time<span class="token punctuation">,</span> immediately</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>immediately <span class="token operator">===</span> <span class="token keyword">undefined</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        immediately <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>immediately<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 立即执行，第二次在一段时间后执行一次</span>
        <span class="token keyword">var</span> t<span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>immediately<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>t <span class="token operator">||</span> Date<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> t <span class="token operator">&gt;=</span> time<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//之前没有计时 或 距离上次执行的时间已超过规定的值</span>
                    <span class="token function">callback</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    t <span class="token operator">=</span> Date<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//得到的当前时间戳</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token comment">// 保证一段时间后只执行一次</span>
        <span class="token keyword">var</span> timer<span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>timer<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 有计时器了直接返回</span>
                <span class="token keyword">return</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">var</span> args <span class="token operator">=</span> arguments<span class="token punctuation">;</span> <span class="token comment">//利用闭包保存参数数组</span>
            timer <span class="token operator">=</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">callback</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
                timer <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token comment">//计时器清空</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span> time<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><hr> <h2 id="debounce"><a href="#debounce" class="header-anchor">#</a> Debounce</h2> <p><strong>函数防抖，就是指触发事件后在 n 秒内函数只能执行一次，如果在 n 秒内又触发了事件，则会重新计算函数执行时间。</strong></p> <p>简单的说，<code>当一个动作连续触发，则只执行最后一次</code>。</p> <p>打个比方，坐公交，司机需要等最后一个人进入才能关门。每次进入一个人，司机就会多等待几秒再关门。<code>上电梯</code></p> <h3 id="函数防抖的应用场景"><a href="#函数防抖的应用场景" class="header-anchor">#</a> 函数防抖的应用场景</h3> <p>连续的事件，只需触发一次回调的场景有：</p> <ul><li>搜索框搜索输入。只需用户最后一次输入完，再发送请求</li> <li>手机号、邮箱验证输入检测</li> <li>窗口大小Resize。只需窗口调整完成后，计算窗口大小。防止重复渲染。</li></ul> <h3 id="代码-3"><a href="#代码-3" class="header-anchor">#</a> 代码</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * 函数防抖
 */</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin<span class="token punctuation">.</span><span class="token function-variable function">debounce</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">callback<span class="token punctuation">,</span> time</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> timer<span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">clearTimeout</span><span class="token punctuation">(</span>timer<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//清除之前的计时</span>
        <span class="token keyword">var</span> args <span class="token operator">=</span> arguments<span class="token punctuation">;</span> <span class="token comment">//利用闭包保存参数数组</span>
        timer <span class="token operator">=</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">callback</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> time<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><strong>例1：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code>            <span class="token comment">//高阶函数：在函数内部返回一个新的函数</span>
        <span class="token comment">//函数防抖: debounce</span>
        <span class="token keyword">var</span> handle <span class="token operator">=</span> myPlugin<span class="token punctuation">.</span><span class="token function">debounce</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">width</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>width<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 改变窗口大小</span>
        window<span class="token punctuation">.</span><span class="token function-variable function">onresize</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">handle</span><span class="token punctuation">(</span>document<span class="token punctuation">.</span>documentElement<span class="token punctuation">.</span>clientWidth<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// 搜索框触发</span>
        <span class="token keyword">var</span> inp <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">&quot;input&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> handle <span class="token operator">=</span> myPlugin<span class="token punctuation">.</span><span class="token function">debounce</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">val<span class="token punctuation">,</span> inp</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;搜索&quot;</span> <span class="token operator">+</span> val<span class="token punctuation">)</span><span class="token punctuation">;</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>inp<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// &lt;input type=&quot;text&quot;&gt;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        inp<span class="token punctuation">.</span><span class="token function-variable function">oninput</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">handle</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
</code></pre></div><h2 id="clone"><a href="#clone" class="header-anchor">#</a> Clone</h2> <p><strong>克隆</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * 克隆一个对象
 * @param {boolean} deep 是否深度克隆 不传默认是浅度克隆
 */</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin<span class="token punctuation">.</span><span class="token function-variable function">clone</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">obj<span class="token punctuation">,</span> deep</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">//判断数组</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>deep<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">//深度克隆</span>
            <span class="token comment">// 如果数组中还有引用值类型，引用地址相等则不适用，用深度克隆</span>
            <span class="token keyword">var</span> newArr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> obj<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                newArr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span>obj<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> deep<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> newArr<span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token comment">// 浅度克隆 </span>
            <span class="token keyword">return</span> obj<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//复制数组</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> obj <span class="token operator">===</span> <span class="token string">&quot;object&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">//判断对象</span>
        <span class="token keyword">var</span> newObj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> prop <span class="token keyword">in</span> obj<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>deep<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">//深度克隆</span>
                newObj<span class="token punctuation">[</span>prop<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span>obj<span class="token punctuation">[</span>prop<span class="token punctuation">]</span><span class="token punctuation">,</span> deep<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                <span class="token comment">// 浅度克隆 如果对象中还有引用值类型，引用地址相等则不适用，用深度克隆</span>
                newObj<span class="token punctuation">[</span>prop<span class="token punctuation">]</span> <span class="token operator">=</span> obj<span class="token punctuation">[</span>prop<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> newObj<span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token comment">//判断原始值 函数类型</span>
        <span class="token keyword">return</span> obj<span class="token punctuation">;</span> <span class="token comment">//递归的终止条件</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><strong>例1：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code>      <span class="token comment">// 数组</span>
        <span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">,</span> <span class="token number">34</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
            a<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
            b<span class="token operator">:</span> <span class="token number">2</span>
        <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token comment">// 浅层克隆</span>
        <span class="token keyword">var</span> newarr <span class="token operator">=</span> Plugin<span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newarr<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">===</span> arr<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">//true </span>

        <span class="token comment">// 深层克隆</span>
        <span class="token keyword">var</span> newarr1 <span class="token operator">=</span> Plugin<span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newarr1<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">===</span> arr<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">//false</span>
</code></pre></div><p><strong>例2：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code>        <span class="token comment">// 对象</span>
        <span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
                a<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                b<span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
                sum<span class="token operator">:</span> <span class="token punctuation">{</span>
                    c<span class="token operator">:</span> <span class="token number">34</span><span class="token punctuation">,</span>
                    d<span class="token operator">:</span> <span class="token number">34</span>
                <span class="token punctuation">}</span><span class="token punctuation">,</span>
                arr<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
                    a<span class="token operator">:</span> <span class="token number">7</span>
                <span class="token punctuation">}</span><span class="token punctuation">]</span>
            <span class="token punctuation">}</span>
            <span class="token comment">// 浅层克隆</span>
        <span class="token keyword">var</span> newobj <span class="token operator">=</span> Plugin<span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newobj<span class="token punctuation">.</span>sum <span class="token operator">===</span> obj<span class="token punctuation">.</span>sum<span class="token punctuation">)</span><span class="token comment">//true</span>

        <span class="token comment">// 深层克隆</span>
        <span class="token keyword">var</span> newobj1 <span class="token operator">=</span> Plugin<span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newobj1<span class="token punctuation">.</span>arr<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">===</span> obj<span class="token punctuation">.</span>arr<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">//false</span>
</code></pre></div><h2 id="mixin"><a href="#mixin" class="header-anchor">#</a> Mixin</h2> <p><strong>混合：两个对象，混合后产生一个新对象 同名覆盖，不同则添加</strong></p> <div class="language-js extra-class"><pre class="language-js"><code>
        <span class="token keyword">var</span> obj1 <span class="token operator">=</span> <span class="token punctuation">{</span>
            x<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
            y<span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
            z<span class="token operator">:</span> <span class="token number">5</span>
        <span class="token punctuation">}</span><span class="token punctuation">;</span>

        <span class="token keyword">var</span> obj2 <span class="token operator">=</span> <span class="token punctuation">{</span>
            x<span class="token operator">:</span> <span class="token string">&quot;abc&quot;</span><span class="token punctuation">,</span>
            z<span class="token operator">:</span> <span class="token string">&quot;bcd&quot;</span><span class="token punctuation">,</span>
            h<span class="token operator">:</span> <span class="token string">&quot;aaa&quot;</span>
        <span class="token punctuation">}</span><span class="token punctuation">;</span>

        <span class="token comment">// obj2混合到obj1,同名覆盖，不同则添加</span>
        <span class="token keyword">var</span> obj <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> obj1<span class="token punctuation">,</span> obj2<span class="token punctuation">)</span><span class="token punctuation">;</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//{x: &quot;abc&quot;, y: 3, z: &quot;bcd&quot;, h: &quot;aaa&quot;}</span>

        <span class="token keyword">var</span> obj <span class="token operator">=</span> myPlugin<span class="token punctuation">.</span><span class="token function">mixin</span><span class="token punctuation">(</span>obj1<span class="token punctuation">,</span> obj2<span class="token punctuation">)</span><span class="token punctuation">;</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//{x: &quot;abc&quot;, y: 3, z: &quot;bcd&quot;, h: &quot;aaa&quot;}</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code>       <span class="token function">complicate</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
            a<span class="token operator">:</span> <span class="token number">123</span><span class="token punctuation">,</span>
            b<span class="token operator">:</span> <span class="token number">34</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//option参数是一个对象，表示配置对象</span>
        <span class="token keyword">function</span> <span class="token function">complicate</span><span class="token punctuation">(</span><span class="token parameter">option</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">//如果没有传递，则使用默认值</span>
            <span class="token keyword">var</span> defaultOption <span class="token operator">=</span> <span class="token punctuation">{</span>
                a<span class="token operator">:</span> <span class="token string">&quot;default-a&quot;</span><span class="token punctuation">,</span>
                b<span class="token operator">:</span> <span class="token string">&quot;default-b&quot;</span><span class="token punctuation">,</span>
                c<span class="token operator">:</span> <span class="token string">&quot;default-c&quot;</span><span class="token punctuation">,</span>
                d<span class="token operator">:</span> <span class="token string">&quot;default-d&quot;</span><span class="token punctuation">,</span>
                e<span class="token operator">:</span> <span class="token string">&quot;default-e&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">;</span>

            <span class="token keyword">var</span> opt <span class="token operator">=</span> myPlugin<span class="token punctuation">.</span><span class="token function">mixin</span><span class="token punctuation">(</span>defaultOption<span class="token punctuation">,</span> option<span class="token punctuation">)</span><span class="token punctuation">;</span>

            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>opt<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//{a: 123, b: 34, c: &quot;default-c&quot;, d: &quot;default-d&quot;, e: &quot;default-e&quot;}</span>
</code></pre></div><p><strong>代码：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * obj2混合到obj1产生新的对象
 */</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>myPlugin<span class="token punctuation">.</span><span class="token function-variable function">mixin</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">obj1<span class="token punctuation">,</span> obj2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 简易写法</span>
    <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> obj1<span class="token punctuation">,</span> obj2<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 普通写法</span>
    <span class="token comment">// var newObj = {};</span>
    <span class="token comment">// //复制obj2的属性</span>
    <span class="token comment">// for (var prop in obj2) {</span>
    <span class="token comment">//     newObj[prop] = obj2[prop];</span>
    <span class="token comment">// }</span>
    <span class="token comment">// //找到obj1中有但是obj2中没有的属性</span>
    <span class="token comment">// for (var prop in obj1) {</span>
    <span class="token comment">//     if (!(prop in obj2)) {</span>
    <span class="token comment">//         newObj[prop] = obj1[prop];</span>
    <span class="token comment">//     }</span>
    <span class="token comment">// }</span>
    <span class="token comment">// return newObj;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="scope"><a href="#scope" class="header-anchor">#</a> Scope</h2> <p><strong>作用域链</strong></p> <ol><li>VO中包含一个额外的属性，该属性指向创建该VO的函数本身</li> <li>每个函数在创建时，会有一个隐藏属性<code>[[scope]]</code>，它指向创建该函数时的AO</li> <li>当访问一个变量时，会先查找自身VO中是否存在，如果不存在，则依次查找<code>[[scope]]</code>属性。</li></ol> <p>某些浏览器==会优化作用域链==，函数的<code>[[scope]]</code>中仅<code>保留需要用到的数据</code>。</p> <div class="language-js extra-class"><pre class="language-js"><code>例<span class="token number">1</span>：
        <span class="token keyword">var</span> g <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

        <span class="token keyword">function</span> <span class="token constant">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>

            <span class="token keyword">function</span> <span class="token constant">B</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
                <span class="token keyword">var</span> <span class="token function-variable function">C</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
                    <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>内层函数变量没有，就会向外层函数寻找，直到找到为止，如果不存在，则返回<span class="token keyword">undefined</span><span class="token operator">--</span><span class="token operator">&gt;</span>
                    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> b<span class="token punctuation">,</span> a<span class="token punctuation">,</span> g<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 3 2 1 0</span>
                <span class="token punctuation">}</span>
                <span class="token constant">C</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token constant">B</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token constant">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code>例<span class="token number">2</span>：
      <span class="token keyword">var</span> count <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">;</span>
        <span class="token keyword">function</span> <span class="token constant">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">var</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                count<span class="token operator">++</span><span class="token punctuation">;</span>
                console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">var</span> test <span class="token operator">=</span> <span class="token constant">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//1</span>
        <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//2</span>
        <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//3</span>

        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//100</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code>例<span class="token number">3</span>：
     <span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>

        <span class="token comment">// 声明A函数，a的值由最内层的作用域决定</span>
        <span class="token keyword">function</span> <span class="token constant">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//1 而不能取到5</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">function</span> <span class="token function">Special</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
            <span class="token keyword">var</span> <span class="token constant">B</span> <span class="token operator">=</span> <span class="token constant">A</span><span class="token punctuation">;</span>
            <span class="token constant">B</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token function">Special</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><hr> <h4 id="面试题"><a href="#面试题" class="header-anchor">#</a> 面试题：</h4> <p><strong>例1：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span> n<span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">foo</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>foo<span class="token punctuation">.</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1、</span>
    <span class="token comment">// 如果变量未经声明就赋值，此变量就为全局对象所有</span>
    foo<span class="token punctuation">.</span>n <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span> n<span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>foo<span class="token punctuation">.</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 2</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>foo<span class="token punctuation">.</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 3</span>
</code></pre></div><p><strong>例2：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> food <span class="token operator">=</span> <span class="token string">&quot;rice&quot;</span><span class="token punctuation">;</span>
<span class="token comment">// 此food变量由声明eat函数最近作用链决定</span>
<span class="token keyword">var</span> <span class="token function-variable function">eat</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">eat </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>food<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> food <span class="token operator">=</span> <span class="token string">&quot;noodle&quot;</span><span class="token punctuation">;</span>
    <span class="token function">eat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//eat rice</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>例3：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> food <span class="token operator">=</span> <span class="token string">&quot;rice&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> food <span class="token operator">=</span> <span class="token string">&quot;noodle&quot;</span><span class="token punctuation">;</span>
    <span class="token comment">// 此food变量由声明eat函数最近作用链决定</span>
    <span class="token keyword">var</span> <span class="token function-variable function">eat</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">eat </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>food<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token function">eat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//eat noodle</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>例4：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token constant">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//10个10  运行顺序 1</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token constant">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//undefined  运行顺序 0 值越小运行时间越靠前</span>


<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">3</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">i</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 0 1 2 运行顺序： 2</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div></div> <footer class="page-edit" style="display:none;"><div class="edit-link"><a href="https://github.com/zscnb/blog/edit/master/docs/views/blog/Sao-operation.md" target="_blank" rel="noopener noreferrer">HELP ME</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time"> 2021/03/28 13:30:56</span></div></footer> <!----> <!----> <!----></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><!----><!----><div class="copydialog" style="display:none;" data-v-31384c7b data-v-31384c7b><div class="dislog-mask" data-v-31384c7b></div> <div class="copy-limit-dialog" data-v-31384c7b><div class="close-dialog-btn" data-v-31384c7b></div> <div class="main-content" data-v-31384c7b><span class="tip-message" data-v-31384c7b>
          文档复制为VIP权益，开通VIP可继续复制
        </span> <ul class="list" data-v-31384c7b><li class="xztq-r" data-v-31384c7b>享全站VIP文档下载特权</li> <li class="mfzx-r" data-v-31384c7b>与博主亲密交流特权</li> <li class="eight-count-r" data-v-31384c7b>
            随时随地随心下载
          </li> <li class="gszh-r" data-v-31384c7b>文档多格式转化</li></ul> <span class="buy-vip-btn" data-v-31384c7b><a href="https://ftp.bmp.ovh/imgs/2021/05/dd79da2e8a65b1d5.jpg" target="_blank" rel="noopener noreferrer" data-v-31384c7b>开通VIP, 享受无限制复制特权</a></span></div></div></div><div class="box" style="display:none;" data-v-bb7a82ec>
  春节快乐
  <div class="light" data-v-bb7a82ec><div class="line" data-v-bb7a82ec></div> <div class="light-a" data-v-bb7a82ec><div class="light-b" data-v-bb7a82ec></div> <div class="light-c" data-v-bb7a82ec>
        新
      </div> <div class="ear-a" data-v-bb7a82ec><div class="ear-b" data-v-bb7a82ec></div> <div class="ear-c" data-v-bb7a82ec></div></div></div></div></div><div data-v-50ef6c52><div class="DetailsOpenFlag" style="right:1rem;bottom:9rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;font-size:14px;font-weight:500;display:none;" data-v-50ef6c52>
 展开 

</div></div><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="right:68px;bottom:190px;display:none;" data-v-5775ee02>
      欢迎来到 青灯有味
    </div> <div class="operation" style="right:56px;bottom:42px;display:;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:none;" data-v-5775ee02></i></div> <canvas id="banniang" width="150" height="220" class="live2d" style="right:56px;bottom:-8px;opacity:1;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><div class="reco-bgm-panel" data-v-39f9e6e0><audio id="bgm" src="http://zscnb.gitee.io/upload/static/audio/五里 - 乌兰巴托的夜.mp3" data-v-39f9e6e0></audio> <div class="reco-float-box" style="bottom:200px;z-index:999999;display:none;" data-v-39f9e6e0 data-v-41bcba48 data-v-39f9e6e0><img src="http://zscnb.gitee.io/upload/static/img/wulan.png" data-v-39f9e6e0></div> <div class="reco-bgm-box" style="left:10px;bottom:10px;z-index:999999;" data-v-39f9e6e0 data-v-41bcba48 data-v-39f9e6e0><div class="reco-bgm-cover" style="background-image:url(http://zscnb.gitee.io/upload/static/img/wulan.png);" data-v-39f9e6e0><div class="mini-operation" style="display:none;" data-v-39f9e6e0><i class="reco-bgm reco-bgm-pause" style="display:none;" data-v-39f9e6e0></i> <i class="reco-bgm reco-bgm-play" style="display:none;" data-v-39f9e6e0></i></div> <div class="falut-message" style="display:none;" data-v-39f9e6e0>
          播放失败
        </div></div> <div class="reco-bgm-info" data-v-39f9e6e0 data-v-41bcba48 data-v-39f9e6e0><div class="info-box" data-v-39f9e6e0><i class="reco-bgm reco-bgm-music music" data-v-39f9e6e0></i>五里-乌兰巴托的夜</div> <div class="info-box" data-v-39f9e6e0><i class="reco-bgm reco-bgm-artist" data-v-39f9e6e0></i>乌兰巴托的夜</div> <div class="reco-bgm-progress" data-v-39f9e6e0><div class="progress-bar" data-v-39f9e6e0><div class="bar" data-v-39f9e6e0></div></div></div> <div class="reco-bgm-operation" data-v-39f9e6e0><i class="reco-bgm reco-bgm-last last" data-v-39f9e6e0></i> <i class="reco-bgm reco-bgm-pause pause" style="display:none;" data-v-39f9e6e0></i> <i class="reco-bgm reco-bgm-play play" data-v-39f9e6e0></i> <i class="reco-bgm reco-bgm-next next" data-v-39f9e6e0></i> <i class="reco-bgm reco-bgm-volume1 volume" data-v-39f9e6e0></i> <i class="reco-bgm reco-bgm-mute mute" style="display:none;" data-v-39f9e6e0></i> <div class="volume-bar" data-v-39f9e6e0><div class="bar" data-v-39f9e6e0></div></div></div></div> <div class="reco-bgm-left-box" data-v-39f9e6e0 data-v-41bcba48 data-v-39f9e6e0><i class="reco-bgm reco-bgm-left" data-v-39f9e6e0></i></div></div></div><div></div><div id="musicPlayer" data-v-b7b63636><!----> <div class="bbox" data-v-b7b63636><div class="pan" style="background-image:url(/assets/img/pan.07613e22.png);" data-v-b7b63636><img src="" alt class="pan_c" data-v-b7b63636></div> <div class="box" style="background-image:url();" data-v-b7b63636><div class="music_shlter_2" style="background-image:url();" data-v-b7b63636></div> <div class="music_shlter" style="background-image:url();" data-v-b7b63636></div> <div class="music_shlter_3" data-v-b7b63636></div> <div class="music_dis" data-v-b7b63636><div class="dis_list" data-v-b7b63636>···</div> <p class="music_title" data-v-b7b63636></p> <p class="music_intro" data-v-b7b63636>歌手: </p> <ul class="music_words" data-v-b7b63636><div class="music_words_box" style="top:0px;" data-v-b7b63636></div></ul></div> <div class="control_box" data-v-b7b63636><div class="control_button" data-v-b7b63636><img src="" alt class="control_icon" data-v-b7b63636></div> <div class="progress" data-v-b7b63636><div class="progress_c" style="width:0%;" data-v-b7b63636><div class="progress_circle" data-v-b7b63636><div class="progress_circle_c" data-v-b7b63636></div></div></div></div></div></div> <video id="music" autoplay="autoplay" src="" name="media" data-v-b7b63636></video></div></div></div></div>
    <script src="/assets/js/app.3af45ec9.js" defer></script><script src="/assets/js/6.5ffa3a3c.js" defer></script><script src="/assets/js/1.60e80d20.js" defer></script><script src="/assets/js/38.27cec8e2.js" defer></script><script src="/assets/js/36.10d521da.js" defer></script>
  </body>
</html>
